package com.common.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

/**
 * @author yz
 * @data: 2022/4/7 13:03 星期四
 * @file : MD5Utils.java
 */


public class MD5Utils {

    //    默认盐
    private static final String SALT = "@a2b3!4d*H";
    // 前端加密盐
    private static final String YAN = "!a@n%p&";


    @NotNull
    public static String md5(String str) {
        return DigestUtils.md5Hex(str);
    }


    /**
     * 生成前端加密 密码
     */

    @NotNull
    public static String inputPassQiAnYan(String qi) {
        return md5(YAN + qi + YAN);
    }

    /**
     * 默认盐 后端加密
     */

    @NotNull
    public static String inputPassToFromPass(String inputPass) {
        String str = SALT.charAt(0) + SALT.charAt(6) + SALT.charAt(2) + inputPass + SALT.charAt(4) + SALT.charAt(1) + SALT.charAt(9);
        return md5(str);
    }

    /**
     * 后端随机盐加密
     */
    public static String houInputPassToFromPass(String inputPass, String salt) {
        String str = salt.charAt(0) + salt.charAt(6) + salt.charAt(2) + inputPass + salt.charAt(4) + salt.charAt(1) + salt.charAt(9);
        return md5(str);
    }


    @Test
    public void test() {
        String password = inputPassQiAnYan("1234");
        System.out.println("inputPassQiAnYan(\"1234\") = " + password);
        System.out.println("inputPassToFromPass(password) = " + inputPassToFromPass(password));
        String salt = CodeUtil.salt();
        System.out.println("salt = " + salt);
        System.out.println("houInputPassToFromPass(password,CodeUtil.salt()) = " + houInputPassToFromPass(password, salt));
    }


}
